package tw.BBT.BasalBodyTemperature.Components.Common;

import tw.BBT.BasalBodyTemperature.BasalBodyTemperature;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class TemperatureStatisticDraw {

	SurfaceView TemperatureStatistic;
	SurfaceHolder surfaceHolder;
	Canvas_Callback CCA;
	BasalBodyTemperature BBT;
	Paint Table_paint, Line_paint,Circle_paint;
	
	// Number of row/col
	int RowNum = 19;
	int ColNum = 8;
	
	// Reserved space for the  scale of x/y-axis
	double CanvasLocation_x = 30;
	double CanvasLocation_y = 20;
	
	// The base value of X/YScale
	int XScale = 0;
	double YScale = 36;
	
	// Location-offset for the value of X/YScale
	int XScaleOffset = -5;
	int YScaleOffset = 3;
	
	// Weight/Height per Col/Raw
	double ColSize, RowSize ;
	
	double StatisticGraph_Wei, StatisticGraph_Hei;

	public TemperatureStatisticDraw(BasalBodyTemperature BBT,
			SurfaceView TemperatureStatistic) {
		this.BBT = BBT;
		this.TemperatureStatistic = TemperatureStatistic;
		CCA = new Canvas_Callback();
		surfaceHolder = TemperatureStatistic.getHolder();
		surfaceHolder.addCallback(CCA);
		StatisticGraph_Wei = TemperatureStatistic.getLayoutParams().width - CanvasLocation_x ;
		StatisticGraph_Hei = TemperatureStatistic.getLayoutParams().height - CanvasLocation_y ;
		RowSize = (double)StatisticGraph_Hei/(double)RowNum;
		ColSize = (double)StatisticGraph_Wei/(double)ColNum;

	}
	
	public enum DrawAction {
		DrawInit, StatisticDraw,
	}
	
	class Canvas_Callback implements Callback {

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			Draw(DrawAction.DrawInit);
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}

	public void Draw(DrawAction action) {
		Canvas canvas = null;
		try {
			canvas = surfaceHolder.lockCanvas();
			synchronized (surfaceHolder) {
				switch (action) {
				case DrawInit:
					DrawInit(canvas);
					break;
				case StatisticDraw:
					drawAndroid(canvas);
					break;
				}
			}
		} finally {
			if (canvas != null) {
				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}
	}

	void drawAndroid(Canvas canvas) {

		int x, y ;
		int DBRow_count=0;
		
		double temprature_curr,temprature_prev;
		double Point_curr_x, Point_curr_y, Point_prev_x, Point_prev_y;
		
		DBhelper dbh = new DBhelper(BBT);
		SQLiteDatabase wrdb = dbh.getWritableDatabase();
		Cursor cursor = wrdb.rawQuery("select * from BBTData;", null);
		
		canvas.drawRect((float)CanvasLocation_x, 0, (float)(CanvasLocation_x + StatisticGraph_Wei-1), (float)(StatisticGraph_Hei-1), Table_paint);
		
		// Draw x-axis
		for (y = 0; y <= RowNum; y ++) {
			if(y > 0 && y < RowNum ) {
				canvas.drawText(String.valueOf(YScale+(double)y*0.1) , 0, (float)((StatisticGraph_Hei-y*RowSize+YScaleOffset)), Table_paint);
			}
			canvas.drawLine((int)CanvasLocation_x-2, (int)(y*RowSize), (int)(CanvasLocation_x + 4), (int)(y*RowSize), Table_paint);
		}	

		//Draw y-axis
		for (x = 0; x <= ColNum; x ++) {
			if(x > 0 && x < ColNum) {
				canvas.drawText(String.valueOf(XScale+(double)x) , (float)((x*ColSize+CanvasLocation_x+XScaleOffset)), (float)(CanvasLocation_y + StatisticGraph_Hei), Table_paint);
			}
			canvas.drawLine((int)(x*ColSize+CanvasLocation_x), (int)(StatisticGraph_Hei-4), (int)(x*ColSize+CanvasLocation_x), (int)(StatisticGraph_Hei+2), Table_paint);
		}

		// Draw statistic-nodes
		temprature_prev=0;
		while (cursor.moveToNext()) {
			
			temprature_curr = cursor.getDouble(0) ;
			
			Point_curr_x = CanvasLocation_x+(DBRow_count+1)*(ColSize);
			Point_curr_y = StatisticGraph_Hei-(temprature_curr-YScale)*10*(RowSize);
			canvas.drawCircle((int)Point_curr_x, (int)Point_curr_y, 6, Circle_paint);
			
			if(DBRow_count>=1) {
				
				Point_prev_x = CanvasLocation_x+(DBRow_count)*(ColSize);
				Point_prev_y = StatisticGraph_Hei-(temprature_prev-YScale)*10*(RowSize);

				canvas.drawLine((int)Point_prev_x,(int)Point_prev_y,(int)Point_curr_x,(int)Point_curr_y,Line_paint); 
			}
			
			DBRow_count++;
			temprature_prev =  temprature_curr;
		}
		
		cursor.close();
		wrdb.close();
		
		canvas.save();
		canvas.restore();
	}

	void DrawInit(Canvas canvas) {

		Table_paint = new Paint();
		Table_paint.setColor(Color.WHITE);
		Table_paint.setStyle(Paint.Style.STROKE);
		
		Line_paint = new Paint();
		Line_paint.setColor(Color.RED);
		Line_paint.setStyle(Paint.Style.STROKE);
		
		Circle_paint = new Paint();
		Circle_paint.setColor(Color.RED);

		drawAndroid(canvas);
	}
}
